﻿Module ProjectFunctions

    Public Sub PublishProject(ByVal strProject As String)
        Try
            If (strProject = "") Then Return
            If (frmMain.OpenProjects.ContainsKey(strProject) = False) Then Return

            Dim ParsedLines As New ArrayList

            SyntaxCheck.CheckSyntax(strProject, ParsedLines)

            Dim strFilepath As String = CType(frmMain.OpenProjects(strProject), Project).strFilepath
            strFilepath = strFilepath.Replace(".nbd", ".txt")

            Dim Stream As New IO.FileStream(strFilepath, IO.FileMode.Create)
            Dim Writer As New IO.StreamWriter(Stream)

            frmMain.pbProgress.Maximum = ParsedLines.Count()
            frmMain.pbProgress.Value = 0

            For Each s As String In ParsedLines
                Writer.WriteLine(s)
                Writer.Flush()

                frmMain.pbProgress.Value += 1
            Next

            Stream.Close()
        Catch ex As Exception
            frmMain.HandleException(ex)
        End Try
    End Sub

    Public Sub OpenProject(ByVal strFilepath As String)
        Try
            Dim strName As String = frmMain.ExtractFilename(strFilepath)

            If (frmMain.OpenProjects.Contains(strName) = False) Then
                frmMain.ProgramState = frmMain.State.OpeningFile

                Dim tpTemp As New TabPage
                Dim txtEditArea As New RichTextBox

                tpTemp.Name = "tp" + strName
                tpTemp.Text = strName

                txtEditArea.Name = "txtEditArea"
                txtEditArea.AcceptsTab = True
                txtEditArea.Width = 782
                txtEditArea.Height = 495
                txtEditArea.Left = 0
                txtEditArea.Top = 0
                txtEditArea.MaxLength = Integer.MaxValue
                txtEditArea.ScrollBars = RichTextBoxScrollBars.Both
                txtEditArea.WordWrap = False

                AddHandler txtEditArea.TextChanged, AddressOf frmMain.EditAreaChanged
                AddHandler txtEditArea.SelectionChanged, AddressOf frmMain.EditAreaSelectionChanged

                tpTemp.Controls.Add(txtEditArea)

                frmMain.tcMain.TabPages.Add(tpTemp)

                frmMain.lblLines.Text = "Lines: " + txtEditArea.Lines.Count.ToString()
                frmMain.tcMain.SelectedTab = tpTemp

                frmMain.OpenProjects.Add(strName, New Project(strName, strFilepath))

                Dim Reader As New IO.StreamReader(strFilepath)

                Dim strFirstLine As String = ""

                If (Reader.EndOfStream = False) Then strFirstLine = Reader.ReadLine()

                ' Convert old project files to new rtf based ones if they are not in the new format
                If (strFirstLine.StartsWith("{\rtf") = False Or strFirstLine = "") Then
                    If (strFirstLine <> "") Then txtEditArea.Text += strFirstLine + vbNewLine

                    While (Reader.EndOfStream = False)
                        txtEditArea.Text += Reader.ReadLine() + vbNewLine
                    End While

                    CType(frmMain.OpenProjects(strName), Project).UpdateEvents(True)

                    Reader.Close()
                    CType(frmMain.OpenProjects(strName), Project).Save(strFilepath)
                Else
                    txtEditArea.LoadFile(strFilepath)
                    CType(frmMain.OpenProjects(strName), Project).UpdateEvents()
                End If

                Reader.Close()

                frmMain.UpdateEventList(frmMain.tcMain.SelectedTab.Text)
                frmMain.UpdateTodoList(frmMain.tcMain.SelectedTab.Text)
            End If

            frmMain.ProgramState = frmMain.State.Idle

        Catch ex As Exception
            frmMain.HandleException(ex)
        End Try
    End Sub
End Module
